package com.aaa.Tool;

import com.aaa.Constants;

import java.io.*;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//  java 获取指定目录里面的所有文件,  输出完整路径 , 不包括子目录
//  C:\Users\Administrator\Desktop\backup\338.txt

public class TXT_file_tools {

    /*
    获取完整路径
    需要先转成 File 类型
        File folder = new File(folder_path);  //  folder_path string 类型
        if (folder.exists() && folder.isDirectory())
    */
    public HashSet<String> get_all_file_path(File directory) {
        // 获取目录中的所有文件
        File[] files = directory.listFiles();
        HashSet<String> file_path_set = new HashSet<>();
        // 遍历文件列表
        assert files != null;
        for (File file : files) {
            // 如果是文件，输出文件路径
            if (file.isFile()) {
//                System.out.println(file.getAbsolutePath());
                file_path_set.add(file.getAbsolutePath());
            }
        }
        return file_path_set;
    }

    public String get_txt_file_title(String file_path){
        String txt_file_data = get_txt_file_data(file_path);
        Split_str split_str = new Split_str();
        String[] strings = txt_file_data.split(split_str.strrrrrr);
        return strings[0];
    }


    public String get_txt_file_data(String file_path) {
        File file = new File(file_path);
        long filelength = file.length();     //获取文件长度
        byte[] filecontent = new byte[(int) filelength];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(filecontent);
    }

    public String get_txt_folder_path() {
        Constants constants = new Constants();
        String setting_Path = constants.getSETTING_FILE_PATH();
        return get_txt_file_data(setting_Path);
    }



    // C:/user/abc/aaa/fdgdfg.txt   ==>    fdgdfg.txt
    public String get_name(String filePath) {
        // 创建 File 对象
        File file = new File(filePath);
        // 获取文件名
        String fileName = file.getName();
        // 输出文件名
//        System.out.println("文件名: " + fileName);
        return fileName;
    }



    // 去掉 非英语 单词
    public String get_english_words(String str) {
        StringBuilder englishWords = new StringBuilder();
        Pattern pattern = Pattern.compile("\\b[a-zA-Z]+\\b"); // 匹配英文单词的正则表达式
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            englishWords.append(matcher.group()).append(" ");
        }
        return englishWords.toString().trim();
    }




    public boolean write_to_txt(String file_name, String file_data) {
        String full_file_path = get_txt_folder_path() + '\\' + file_name;
        File f = new File(full_file_path);
        OutputStream out = null;
        try {
            out = new FileOutputStream(f);
            byte[] b = file_data.getBytes();
            out.write(b);
            out.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

}



